package org.leetcode.middle.leetcode142;

import org.algorithm.List.ListNode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution {

    public ListNode detectCycle2(ListNode head){
        ListNode node= head;
        Set<ListNode> set= new HashSet<>();
        while (node!=null){
            if (set.contains(node)){
                return node;
            }else {
                set.add(node);
            }

            node=node.next;
        }
        return null;
    }
    public ListNode detectCycle(ListNode head) {

        Set<ListNode> set = new HashSet<>();

        while (head!=null)
        {
            if (!set.add(head)) {
                return head;
            }

            head=head.next;
       }

        return null;
    }
}
